package 力扣日常刷题.木22二月.第20天226;

/**
 * @author 帅小伙
 * @date 2022/2/26
 * @description
 */
public class Demo166二进制加法 {

    public String addBinary(String a, String b) {
        StringBuffer sb  = new StringBuffer();
        int count = 0;
        for (int i = 0; i < a.length() || i < b.length(); i++) {
            count += i < a.length() ? (a.charAt(a.length() - 1 - i) - '0') : 0;
            count += i < b.length() ? (b.charAt(b.length() - 1 - i) - '0') : 0;
            sb.append((char)(count%2+'0'));
            count /= 2;
        }
        if(count > 0)   sb.append('1');
        return sb.reverse().toString();
    }

}
